package com.umeox.watch.moto.launcher.ui.widget;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.umeox.watch.moto.launcher.R;


/**
 * 支持多个进度条显示
 */
public class MultiProgressBar extends View {
    private static final String TAG = "MultiProgressBar";
    public static final float SWEEP_ANGLE = 3.6f;
    public static final int CIRCLE_INTERVAL = 3;
    private final float mStartAngle = 270;
    public static final String LAYOUT_WIDTH = "layout_width";
    private final int mStrokeWidth;
    private float mStrokeOffset;
    private RectF mRect;
    private Paint mPaint;
    private RectF mRect2;
    private RectF mRect3;

    private float mWidth;
    private float mWidth3;
    private float mWidth2;

    private LinearGradient mFirstShader;
    private LinearGradient mSecondShader;
    private LinearGradient mThirdShader;

    private int mFirstColorBackground;
    private int mSecondColorBackground;
    private int mThirdColorBackground;

    private float mFirstProgress;
    private float mSecondProgress;
    private float mThirdProgress;

    public MultiProgressBar(Context context) {
        this(context, null);
    }

    public MultiProgressBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MultiProgressBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            String attrName = attrs.getAttributeName(i);
            String attrValue = attrs.getAttributeValue(i);
            if (LAYOUT_WIDTH.equals(attrName)) {
                mWidth = Float.parseFloat(attrValue.replaceAll("px", "").replaceAll("dip", ""));
            }
        }

        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
                R.styleable.MultiProgressBar,
                0, 0);

        try {
            mStrokeWidth = a.getDimensionPixelSize(R.styleable.MultiProgressBar_strokeWidth, 6);
        } finally {
            a.recycle();
        }

        mStrokeOffset = mStrokeWidth / 2f;
        mRect = new RectF(mStrokeOffset, mStrokeOffset, mWidth - mStrokeOffset, mWidth - mStrokeOffset);

        float interval_x = mStrokeWidth + CIRCLE_INTERVAL;

        mWidth2 = mWidth - interval_x * 2;
        final float left2 = mStrokeOffset + interval_x;
        final float top2 = left2;
        final float right2 = mWidth2 + interval_x - mStrokeOffset;
        final float bottom2 = right2;
        mRect2 = new RectF(left2, top2, right2, bottom2);

        mWidth3 = mWidth - interval_x * 4;
        final float left3 = mStrokeOffset + interval_x * 2;
        final float top3 = left3;
        final float right3 = mWidth3 + interval_x * 2 - mStrokeOffset;
        final float bottom3 = right3;
        mRect3 = new RectF(left3, top3, right3, bottom3);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(mStrokeWidth);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setColor(0xFFFF0000);
    }

    /**
     * 第一环:步数
     *
     * @param progressFirst 进度值0~100
     */
    public void setFirstProgress(float progressFirst) {
        mFirstProgress = progressFirst;
        invalidate();
    }

    /**
     * 第二环:卡路里
     *
     * @param secondProgress 进度值0~100
     */
    public void setSecondProgress(float secondProgress) {
        mSecondProgress = secondProgress;
        invalidate();
    }

    /**
     * 第三环:距离
     *
     * @param thirdProgress 进度值0~100
     */
    public void setThirdProgress(float thirdProgress) {
        mThirdProgress = thirdProgress;
        invalidate();
    }

    /**
     * 第一环进度颜色值
     *
     * @param firstColors [开始颜色,结束颜色,背景颜色]
     */
    public void setFirstColors(int... firstColors) {
        mFirstColorBackground = firstColors[2];
        mFirstShader = new LinearGradient(mRect.left, mRect.top,
                mRect.left, mRect.bottom, firstColors[0], firstColors[1], Shader.TileMode.MIRROR);
        invalidate();
    }

    public void setSecondColors(int... secondColors) {
        mSecondColorBackground = secondColors[2];
        mSecondShader = new LinearGradient(mRect2.left, mRect2.top,
                mRect2.left, mRect2.bottom, secondColors[0], secondColors[1], Shader.TileMode.MIRROR);
        invalidate();
    }

    public void setThirdColors(int... thirdColors) {
        mThirdColorBackground = thirdColors[2];
        mThirdShader = new LinearGradient(mRect3.left, mRect3.top,
                mRect3.left, mRect3.bottom, thirdColors[0], thirdColors[1], Shader.TileMode.MIRROR);
        invalidate();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        final float cx = mWidth / 2f;
        mPaint.setShader(null);

        mPaint.setColor(mFirstColorBackground);
        canvas.drawCircle(cx, cx, cx - mStrokeOffset, mPaint);

        mPaint.setColor(mSecondColorBackground);
        canvas.drawCircle(cx, cx, mWidth2 / 2f - mStrokeOffset, mPaint);

        mPaint.setColor(mThirdColorBackground);
        canvas.drawCircle(cx, cx, mWidth3 / 2f - mStrokeOffset, mPaint);

        mPaint.setShader(mFirstShader);
        canvas.drawArc(mRect, mStartAngle, SWEEP_ANGLE * mFirstProgress, false, mPaint);

        mPaint.setShader(mSecondShader);
        canvas.drawArc(mRect2, mStartAngle, SWEEP_ANGLE * mSecondProgress, false, mPaint);

        mPaint.setShader(mThirdShader);
        canvas.drawArc(mRect3, mStartAngle, SWEEP_ANGLE * mThirdProgress, false, mPaint);
    }
}
